<?php
namespace app\index\controller\user;

class Head extends \app\index\controller\user\BaseUser{
    public function index(){
        //把用户信息传递给模板
        $data = [];
        return view('user/head',$data);
    }
    public function save(){
        $request = \think\Request::instance();
        $file = $request->file('r_head');
        $data['file'] = $file;
        $validate = new \think\Validate([
            'file|上传头像'  => 'require',
        ]); 
        if (!$validate->check($data)) {
            return $this->error($validate->getError());
        }           
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate([
                'size'=>1024*1024*10,
                'ext'=>'jpg,png,gif'
                ])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info === false){
            // 上传失败获取错误信息
            return $this->error($file->getError());
        }   
        //对图像进行裁剪
        $fullpath = ROOT_PATH . 'public' . DS . 'uploads' . DS .  $info->getSaveName(); 
        $savepath = ROOT_PATH . 'public' . DS . 'uploads' . DS . "head/{$this->userdata->id}.jpg";
        $image = \think\Image::open($fullpath); 
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png 并且居中裁剪
        $image->thumb(150, 150,\think\Image::THUMB_CENTER)->save($savepath);
        //跳转到当前页
        return $this->redirect('index/user.Head/index');

    }
        
}